package com.fashion.service.gallery;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

import com.fashion.model.gallery.Photo;
import com.fashion.utils.StringUtils;
import com.fashion.utils.Variable;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * Description TODO
 *
 * <br><b>Change Note:</b>
 * <ul>
 * <li>Date:</li>
 * <li>Modifier:</li>
 * <li>Description:</li>
 * </ul>
 *
 * @author TODO created at Jul 2, 2011
 */
@Service("photoService")
public class PhotoService {
    
	@Resource(name = "hibernateTemplate")
	private HibernateTemplate template;
	
	public List<Photo> findByAlbum(String album){
		return template.find("from Photo where album.id = ?", album);
	}
	
	public Photo findById(String id){
	    return template.get(Photo.class, id);
	}
	
	public void addPhoto(Photo photo){	 
	    photo.setId(StringUtils.createGuid());
	    photo.setDate(new Date());
		template.save(photo);
	}
	
	public void delPhoto(Photo photo, String folder){
	    photo = template.load(Photo.class, photo.getId());
	    String path = folder + File.separator + photo.getName();
	    File file = new File(path);
	    file.delete();
	    path = folder + File.separator + Variable.PHOTO_MINI_DIR_SUBSTR + File.separator + photo.getName();
		file = new File(path);
        file.delete();
	    template.delete(photo);
	}
	
	public void savePhoto(InputStream src, String saveName, String saveFolder, boolean autoZoom) throws Exception{
		File folder = new File(saveFolder);
        if(!folder.exists()){
            folder.mkdirs();
        }
        if(autoZoom)
        	savePhoto(src, saveName, saveFolder, Variable.PHOTO_SIZE);     	
        else
        	savePhoto(src, saveName, saveFolder, Integer.MAX_VALUE); 
        src = new FileInputStream(new File(saveFolder + File.separator + saveName));
        saveFolder = saveFolder + File.separator + "mini";       
    	savePhoto(src, saveName, saveFolder, Variable.PHOTO_MINI_SIZE);
	}
	
	public void savePhoto (InputStream src, String saveName, String saveFolder, int size)throws Exception{
	    File folder = new File(saveFolder);
        if(!folder.exists()){
            folder.mkdirs();
        }
	    String dest = saveFolder + File.separator + saveName;
	    Image srcImage = javax.imageio.ImageIO.read(src);
        int oldWidth = srcImage.getWidth(null);
        int oldHeight = srcImage.getHeight(null);
        int newWidth = 0;
        int newHeight = 0;
        BufferedImage tag;
        if (oldWidth < size && oldHeight < size) {
            tag = new BufferedImage(oldWidth, oldHeight,
                    BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(srcImage, 0, 0, oldWidth, oldHeight, null);
        }else{
            if (oldWidth > oldHeight) {
                newWidth = Math.round(size);
                float scale = size / (float)oldWidth;
                newHeight = Math.round(scale * oldHeight);
            } else {
                newHeight = Math.round(size);
                float scale = size / (float)oldHeight;
                newWidth = Math.round(scale * oldWidth);
            }
            tag = new BufferedImage(newWidth, newHeight,
                    BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(srcImage, 0, 0, newWidth, newHeight, null);
        }
        FileOutputStream newImage = new FileOutputStream(dest);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newImage);
        encoder.encode(tag);
        newImage.close();
	}
	
	
	/**
	 * Description tailor photo
	 *
	 */
	public void tailorPhoto(String src, int x, int y, int width, int height)throws Exception{
		File file = new File(src);
	    Image img = javax.imageio.ImageIO.read(file);
        int newWidth = 0;
        int newHeight = 0;
        BufferedImage tag;
        tag = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
        tag.getGraphics().drawImage(img, 0, 0, width, height, x, y, x+width, y+height, null);
        
        FileOutputStream newImage = new FileOutputStream(file);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newImage);
        encoder.encode(tag);
        newImage.close();
	}
}
